﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using Terraria;
using Terraria.Audio;
using Terraria.GameContent;
using Terraria.ID;
using Terraria.ModLoader;

namespace MoLingsArmory.Content.Projectiles
{
    public class FlareGlovePunch : ModProjectile
    {
        public override void SetStaticDefaults()
        {
            ProjectileID.Sets.TrailCacheLength[Projectile.type] = 3;
            ProjectileID.Sets.TrailingMode[Projectile.type] = 0;
        }

        public override bool PreDraw(ref Color lightColor)
        {
            Vector2 drawOrigin = new(TextureAssets.Projectile[Projectile.type].Value.Width * 0.5f, Projectile.height * 0.5f);
            for (int k = 0; k < Projectile.oldPos.Length; k++)
            {
                Vector2 drawPos = Projectile.oldPos[k] - Main.screenPosition + drawOrigin + new Vector2(0f, Projectile.gfxOffY);
                Color color = Projectile.GetAlpha(lightColor) * ((Projectile.oldPos.Length - k) / (float)Projectile.oldPos.Length);
                Main.spriteBatch.Draw(TextureAssets.Projectile[Projectile.type].Value, drawPos, null, color, Projectile.rotation, drawOrigin, Projectile.scale, SpriteEffects.None, 0f);
            }
            return true;
        }

        public override void SetDefaults()
        {
            Projectile.width = 60;
            Projectile.height = 60;
            Projectile.friendly = true;
            Projectile.penetrate = -1;
            Projectile.DamageType = DamageClass.Melee;
            Projectile.extraUpdates = 1;
            Projectile.alpha = 255;
        }
        public override bool TileCollideStyle(ref int width, ref int height, ref bool fallThrough, ref Vector2 hitboxCenterFrac)
        {
            width = 24;
            height = 24;
            return true;
        }

        public override Color? GetAlpha(Color lightColor)
        {
            return new Color(255 - Projectile.alpha, 255 - Projectile.alpha, 255 - Projectile.alpha, 64 - Projectile.alpha / 4);
        }

        public override void AI()
        {
            Projectile.rotation = (float)Math.Atan2(Projectile.velocity.Y, Projectile.velocity.X) + 1.57f;
            if (Projectile.alpha > 40)
            {
                Projectile.alpha -= 15;
            }

            if (Projectile.alpha < 40)
            {
                Projectile.alpha = 40;
            }

            Lighting.AddLight(Projectile.position, 1f, 0.5f, 0f);
            if (Projectile.ai[0] == 0f)
            {
                SoundEngine.PlaySound(SoundID.Item34, Projectile.position);
            }
            if (Projectile.ai[0] > 5f)
            {
                var dustType = Main.rand.Next(3) switch
                {
                    0 => 6,
                    1 => 127,
                    _ => 158,
                };
                int dust = Dust.NewDust(Projectile.position, Projectile.width, Projectile.height, dustType, 0f, 0f, 100, default, 2f);
                Main.dust[dust].noGravity = true;
                Main.dust[dust].velocity *= 0f;
            }
            else
            {
                Projectile.ai[0]++;
            }

            if (Projectile.owner == Main.myPlayer && Projectile.timeLeft <= 3)
            {
                Projectile.tileCollide = false;
                Projectile.ai[1] = 0f;
                Projectile.alpha = 255;

                Projectile.position.X = Projectile.position.X + Projectile.width / 2;
                Projectile.position.Y = Projectile.position.Y + Projectile.height / 2;
                Projectile.width = 150;
                Projectile.height = 150;
                Projectile.position.X = Projectile.position.X - Projectile.width / 2;
                Projectile.position.Y = Projectile.position.Y - Projectile.height / 2;
                Projectile.knockBack = 20f;
            }
        }

        public override void ModifyHitNPC(NPC target, ref NPC.HitModifiers modifiers)
        {
            if (Main.expertMode)
            {
                if (target.type >= NPCID.EaterofWorldsHead && target.type <= NPCID.EaterofWorldsTail)
                {
                    modifiers.SourceDamage /= 5;
                }
            }
        }
        public bool canReduceTime = true;

        public override void OnHitNPC(NPC target, NPC.HitInfo hit, int damageDone)
        {
            if (canReduceTime)
            {
                canReduceTime = false;
                Projectile.timeLeft = 2;
                Projectile.velocity *= 0f;
            }
            Projectile.usesLocalNPCImmunity = true;
            Projectile.localNPCImmunity[target.whoAmI] = 10;
            target.immune[Projectile.owner] = 0;
            target.AddBuff(BuffID.Daybreak, 240, false);
        }

        public override void OnHitPlayer(Player target, Player.HurtInfo info)
        {
            if (canReduceTime)
            {
                canReduceTime = false;
                Projectile.timeLeft = 2;
                Projectile.velocity *= 0f;
            }
            target.AddBuff(BuffID.Daybreak, 240, false);
        }

        public override bool OnTileCollide(Vector2 oldVelocity)
        {
            if (canReduceTime)
            {
                canReduceTime = false;
                Projectile.timeLeft = 2;
            }
            Projectile.velocity *= 0f;
            return false;
        }

        public override void OnKill(int timeLeft)
        {
            Player player = Main.player[Projectile.owner];
            Projectile.position.X = Projectile.position.X + Projectile.width / 2;
            Projectile.position.Y = Projectile.position.Y + Projectile.height / 2;
            Projectile.width = 22;
            Projectile.height = 22;
            Projectile.position.X = Projectile.position.X - Projectile.width / 2;
            Projectile.position.Y = Projectile.position.Y - Projectile.height / 2;
            SoundEngine.PlaySound(SoundID.DD2_KoboldExplosion, Projectile.position);
            for (int i = 0; i < 200; i++)
            {
                var dustType = Main.rand.Next(3) switch
                {
                    0 => 6,
                    1 => 127,
                    _ => 158,
                };
                int dustIndex = Dust.NewDust(Projectile.position, Projectile.width, Projectile.height, dustType, 0f, 0f, 100, default, 2f);
                Main.dust[dustIndex].velocity *= Main.rand.NextFloat(8, 24);
                Main.dust[dustIndex].noGravity = true;
            }
            for (int i = 0; i < 75; i++)
            {
                var dustType = Main.rand.Next(3) switch
                {
                    0 => 6,
                    1 => 127,
                    _ => 158,
                };
                int dustIndex = Dust.NewDust(Projectile.position, Projectile.width, Projectile.height, dustType, 0f, 0f, 100, default, 1.75f);
                Main.dust[dustIndex].velocity *= Main.rand.NextFloat(4, 10);
            }
            for (int i = 0; i < 2; i++)
            {
                Vector2 position66 = new(Projectile.position.X + Projectile.width / 2 - 24f, Projectile.position.Y + Projectile.height / 2 - 24f);
                int num774 = Gore.NewGore(Projectile.GetSource_Death(), position66, default, Main.rand.Next(61, 64), 1f);
                Main.gore[num774].scale = 1.5f;
                Gore gore146 = Main.gore[num774];
                gore146.velocity.X += 1.5f;
                Gore gore147 = Main.gore[num774];
                gore147.velocity.Y += 1.5f;
                Vector2 position67 = new(Projectile.position.X + Projectile.width / 2 - 24f, Projectile.position.Y + Projectile.height / 2 - 24f);
                num774 = Gore.NewGore(Projectile.GetSource_Death(), position67, default, Main.rand.Next(61, 64), 1f);
                Main.gore[num774].scale = 1.5f;
                Gore gore148 = Main.gore[num774];
                gore148.velocity.X -= 1.5f;
                Gore gore149 = Main.gore[num774];
                gore149.velocity.Y += 1.5f;
                Vector2 position68 = new(Projectile.position.X + Projectile.width / 2 - 24f, Projectile.position.Y + Projectile.height / 2 - 24f);
                num774 = Gore.NewGore(Projectile.GetSource_Death(), position68, default, Main.rand.Next(61, 64), 1f);
                Main.gore[num774].scale = 1.5f;
                Gore gore150 = Main.gore[num774];
                gore150.velocity.X += 1.5f;
                Gore gore151 = Main.gore[num774];
                gore151.velocity.Y -= 1.5f;
                Vector2 position69 = new(Projectile.position.X + Projectile.width / 2 - 24f, Projectile.position.Y + Projectile.height / 2 - 24f);
                num774 = Gore.NewGore(Projectile.GetSource_Death(), position69, default, Main.rand.Next(61, 64), 1f);
                Main.gore[num774].scale = 1.5f;
                Gore gore152 = Main.gore[num774];
                gore152.velocity.X -= 1.5f;
                Gore gore153 = Main.gore[num774];
                gore153.velocity.Y -= 1.5f;
            }
        }
    }
}